<?php
// $Id: File.php 27 2010-11-16 12:49:54Z mjohnson60@gmail.com $


class File extends Object
{
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $folder = NULL;
	
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $name = NULL;
	
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $path = NULL;
	
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $lock = NULL;
	
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $handle = NULL;
	
	/**
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $info = array();

	/**
	 * Enter description here ...
	 * @param unknown_type $path
	 * @param unknown_type $create
	 * @param unknown_type $mode
	 */
	function __construct($path, $create = FALSE, $mode = 0755)
	{
		//$this->folder =& new Folder(dirname($path), $create, $mode);
		

		if (!is_dir($path))
		{
			$this->name = basename($path);
			$this->path = realpath(dirname($path));
		}
		
		if ($create && !$this->exists() && $this->safe($path))
		{
			$this->create();
		}
	}

	/* (non-PHPdoc)
	 * @see Object::toString()
	 */
	public function toString()
	{
		return $this->filename();
	}

	/**
	 * @param field_type $folder
	 */
	public function setFolder($folder)
	{
		$this->folder = $folder;
	}

	/**
	 * Enter description here ...
	 * @return NULL
	 */
	public function pwd()
	{
		return $this->path;
	}

	public function filename()
	{
		return $this->path . '/' . $this->name;
	}

	/**
	 * Enter description here ...
	 * @return boolean
	 */
	public function exists()
	{
		return (file_exists($this->path) && is_file($this->path));
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $name
	 * @param unknown_type $ext
	 * @return mixed
	 */
	public function safe($name = NULL, $ext = NULL)
	{
		if (!$name)
		{
			$name = $this->name;
		}
		
		if (!$ext)
		{
			$ext = $this->ext();
		}
		
		return preg_replace("/(?:[^\w\.-]+)/", "_", basename($name, $ext));
	}

	/**
	 * Enter description here ...
	 */
	public function ext()
	{
		if (empty($this->info))
		{
			$this->info();
		}
		
		if (isset($this->info['extension']))
		{
			return $this->info['extension'];
		}
		
		return FALSE;
	}

	/**
	 * Enter description here ...
	 * @return multitype:
	 */
	public function info()
	{
		if (empty($this->info))
		{
			$this->info = pathinfo($this->path);
		}
		
		if (!isset($this->info['filename']))
		{
			$this->info['filename'] = $this->name();
		}
		
		return $this->info;
	}

	/**
	 * Enter description here ...
	 * @return string|NULL|boolean
	 */
	public function name()
	{
		if (empty($this->info))
		{
			$this->info();
		}
		
		if (isset($this->info['extension']))
		{
			return basename($this->name, '.' . $this->info['extension']);
		}
		else
		{
			return $this->name;
		}
		
		return FALSE;
	}

	/**
	 * Enter description here ...
	 * @return boolean
	 */
	public function touch()
	{
		return touch($this->path);
	}

	/**
	 * Enter description here ...
	 * @param unknown_type $dest
	 * @param unknown_type $overwrite
	 * @return boolean
	 */
	public function copy($dest, $overwrite = TRUE)
	{
		if (!$this->exists() || (is_file($dest) && !$overwrite))
		{
			return FALSE;
		}
		
		return copy($this->path, $dest);
	}

	/**
	 * Enter description here ...
	 * @return boolean
	 */
	public function delete()
	{
		clearstatcache();
		if ($this->exists())
		{
			return unlink($this->path);
		}
		
		return FALSE;
	}

	/**
	 * Enter description here ...
	 * @return number|boolean
	 */
	public function size()
	{
		if ($this->exists())
		{
			return filesize($this->path);
		}
		
		return FALSE;
	}

	/**
	 * Enter description here ...
	 * @return number|boolean
	 */
	public function modified()
	{
		if ($this->exists())
		{
			return filemtime($this->path);
		}
		
		return FALSE;
	}
}
